﻿using QLHREntities;
using QLHuongRungBUS;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QLHuongRung
{
    public partial class LapThucDon : Form
    {
        private ThucDonBUS thucdonBUS = new ThucDonBUS();
        private MonAnBUS monanBUS = new MonAnBUS();
        private List<MonAn> thucdonmoi = new List<MonAn>();
        private bool isLoadingComplete = false;
        private bool loadfirst = true;
        private bool loadfirst_thucdon = true;
        public LapThucDon()
        {
            InitializeComponent();
        }

        private void LapThucDon_Load(object sender, EventArgs e)
        {
            SetDataComboBoxThucDon();
            isLoadingComplete = true;
        }

        private void SetDataComboBoxThucDon()
        {
            List<ThucDon> list_thucdon = thucdonBUS.LayDanhSachThucDon();
            var dataThucDon = new BindingList<KeyValuePair<int, string>>();

            foreach (var item in list_thucdon)
            {
                dataThucDon.Add(new KeyValuePair<int, string>(item.MaThucDon, ((DateTime)item.NgayBatDau).ToString("dd/MM/yyyy") + " ~ " + ((DateTime)item.NgayKetThuc).ToString("dd/MM/yyyy")));
            }
            cbbThucDon.DataSource = null;
            cbbThucDon.DataSource = dataThucDon;
            cbbThucDon.ValueMember = "Key";
            cbbThucDon.DisplayMember = "Value";
            cbbThucDon.SelectedIndex = 0;
            isLoadingComplete = true;
        }

        private void onChange_ThayDoiThucDon(object sender, EventArgs e)
        {
            if (isLoadingComplete)
            {
                KeyValuePair<int, string> selectedEntry = (KeyValuePair<int, string>)((ComboBox)sender).SelectedItem;
                int maThucDon = selectedEntry.Key;
                List<MonAn> list_monan = monanBUS.LayDanhSachMonAn(maThucDon);
                if (!loadfirst_thucdon)
                    dGvMonAn.Columns.RemoveAt(2);
                loadfirst_thucdon = false;
                dGvMonAn.DataSource = null;
                dGvMonAn.DataSource = list_monan;
                dGvMonAn.Columns[2].Visible = false;
                dGvMonAn.Columns[3].Visible = false;
                dGvMonAn.Columns[4].Visible = false;
                dGvMonAn.Columns[0].ReadOnly = true;
                dGvMonAn.Columns[1].ReadOnly = true;
                DataGridViewButtonColumn addlButtonColumn = new DataGridViewButtonColumn();
                addlButtonColumn.Text = "Thêm";
                addlButtonColumn.UseColumnTextForButtonValue = true;
                dGvMonAn.Columns.Insert(2, addlButtonColumn);
            }
        }

        private void dGvMonAn_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == ((DataGridView)sender).Columns[2].Index)
            {
                int maMonAn = Int16.Parse(((DataGridView)sender)[0, e.RowIndex].Value.ToString());
                for (int i = 0; i < dGvThucDonMoi.Rows.Count; i++)
                {
                    int maMonAnByRow = Int16.Parse(dGvThucDonMoi[0, i].Value.ToString());
                    if (maMonAn == maMonAnByRow)
                    {
                        MessageBox.Show("Món ăn đã tốn tại trong danh sách thực đơn mới!!!");
                        return;
                    }
                }
                thucdonmoi.Add(monanBUS.LayMonAn(maMonAn));
                if (!loadfirst)
                    dGvThucDonMoi.Columns.RemoveAt(2);
                loadfirst = false;
                dGvThucDonMoi.DataSource = null;
                dGvThucDonMoi.DataSource = thucdonmoi;
                dGvThucDonMoi.Columns[2].Visible = false;
                dGvThucDonMoi.Columns[3].Visible = false;
                dGvThucDonMoi.Columns[4].Visible = false;

                DataGridViewButtonColumn removelButtonColumn = new DataGridViewButtonColumn();
                removelButtonColumn.Text = "Xóa";
                removelButtonColumn.UseColumnTextForButtonValue = true;
                dGvThucDonMoi.Columns.Insert(2, removelButtonColumn);
                MessageBox.Show("Món ăn đã được thêm!!!");
            }
        }

        private void dGvThucDonMoi_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == ((DataGridView)sender).Columns[2].Index)
            {
                int maMonAn = Int16.Parse(((DataGridView)sender)[0, e.RowIndex].Value.ToString());
                for (int i = 0; i < dGvThucDonMoi.Rows.Count; i++)
                {
                    int maMonAnByRow = Int16.Parse(((DataGridView)sender)[0, i].Value.ToString());
                    if (maMonAn == maMonAnByRow)
                    {
                        thucdonmoi.RemoveAt(i);
                    }
                }
                dGvThucDonMoi.Columns.RemoveAt(2);
                dGvThucDonMoi.DataSource = null;
                dGvThucDonMoi.DataSource = thucdonmoi;
                dGvThucDonMoi.Columns[2].Visible = false;
                dGvThucDonMoi.Columns[3].Visible = false;
                dGvThucDonMoi.Columns[4].Visible = false;

                DataGridViewButtonColumn removelButtonColumn = new DataGridViewButtonColumn();
                removelButtonColumn.Text = "Xóa";
                removelButtonColumn.UseColumnTextForButtonValue = true;
                dGvThucDonMoi.Columns.Insert(2, removelButtonColumn);
                MessageBox.Show("Món ăn đã được xóa!!!");
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (dtBatDau.Value > dtKetThuc.Value)
            {
                MessageBox.Show("Ngày bắt đầu không được sau ngày kết thúc!!!");
                return;
            }
            thucdonBUS.ThemThucDonMoi(dtBatDau.Value, dtKetThuc.Value);

            thucdonBUS.ThemChiTietThucDonMoi(thucdonmoi);

            isLoadingComplete = false;
            SetDataComboBoxThucDon();

            MessageBox.Show("Đã thêm thực đơn mới!!!");
        }
    }
}
